/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import { Server, Response, Request, createServer } from "restify";

import {RestResource} from "./rest/RestResource";

export class AppServer {
	private server : Server;

	private restResource : RestResource;
	
	constructor() {
		this.init();
	}
	
	init() {
		console.log("开始创建服务...");
		this.server = createServer();

        console.log("允许跨域访问...");
		this.server.use(this.crossOrigin);
		
		
		console.log("开始创建服务...");

		this.restResource = new RestResource();

        this.server.get('/rest/bingo/find/current', this.restResource.getCurrentBingo);
        this.server.get('/rest/prize/listByBingo/:bingo', this.restResource.getPrizesByBingo);

		this.server.get('/rest/member/listAll', this.restResource.getAllMembers);
        this.server.get('/rest/member/listRemain/:bingo', this.restResource.getMembersWithoutWinner);
        this.server.get('/rest/member/listRemainValid/:bingo/:prize', this.restResource.getMembersWithoutWinnerAndBlack);

        this.server.get('/rest/black/list/:bingo/:prize', this.restResource.getBlacksByBingoPrize);
        this.server.get('/rest/winner/listByBingo/:bingo', this.restResource.getWinnersByBingo);
        this.server.get('/rest/winner/listByPrize/:bingo/:prize', this.restResource.getWinnersByPrize);

        this.server.get('/rest/winner/random/:bingo/:prize', this.restResource.addWinnerByRandom);
        this.server.get('/rest/winner/remove/:winner', this.restResource.removeWinnerById);

		this.server.listen(8090, () => {
			console.log('%s listening at %s', this.server.name, this.server.url);
		});
	}

	crossOrigin(req, res, next){
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        return next();
    }
	
}
